"""
How to create and start multiple threads
How to wait for threads to complete
How to share data between threads
How to use Locks to prevent race conditions
What is a daemon thread
How to use a Queue for thread-safe data/task processing.

- 如何创建和启动多线程
- 如何等待多线程完成
- 多线程间如何分享数据
- 如何使用锁来防止竞态条件
- 什么是守护线程
- 如何使用队列对于线程安全的 数据/任务 进程
"""
# 1. 创建和运行多线程
from threading import Thread


def square_numbers():
    for i in range(1000):
        result = i * i


if __name__ == "__main__":
    threads = []
    num_threads = 10

    # create threads and asign a function for each thread
    # 创建多个线程，并且为每个线程分配一个执行函数
    for i in range(num_threads):
        thread = Thread(target=square_numbers)
        threads.append(thread)

    # start all threads
    # 启动每一个线程
    for thread in threads:
        thread.start()

    # wait for all threads to finish
    # block the main thread until these threads are finished
    # 等待所有的线程结束，阻塞主线程直到所有的线程结束
    for thread in threads:
        thread.join()
        print("等待所有的线程结束，阻塞主线程直到所有的线程结束" + thread.name)
